Url Shortening App in Flask

Star Badge Open Source Love

URL Shortening Application in Flask

๐Ÿ› ๏ธ Description

This project is about developing a url shortening application in Flask and MongoDB. User will paste their long URLs in this application and will get a shortened url, which will redirect to the same long url once used in a browser.

โš™๏ธ Languages or Frameworks Used

  • Flask, MongoDB
  • HTML, CSS, Bootstrap

๐ŸŒŸ How to run

  • Install all the requirements

    Run pip install -r requirements.txt to install all the requirements.
  • MongoDB Setup for Project

    • Download monogdb from the official website and setup in your local system for testing.
    • Once it is setup locally, try creating documents and collections in mongodb to familiarize yourself with it.
    • You can also download the MongoDB Compass, which is the GUI version of Mongo Shell.
    • Once all the local testing is done, you can create a free cloud version of MongoDB in MongoDB Atlas and get the following credentials from the dashboard of atlas:
    export MONGO_URI=YOUR_MONGO_URI
    export MONGO_USERNAME=YOUR_MONGO_USERNAME
    export MONGO_PASSWORD=YOUR_MONGO_PASSWORD
  • Setup Environment for the project

    • Now create a .env file in your project dreictory and include the following parameters as it is :-
    export ENVIRONMENT=local | production (choose on the basis of local or production environment)
    export APP_SECRET=YOUR_APP_SECRET
    export APP_URL=YOUR_APP_URL (the short url)
    export MONGO_URI=YOUR_MONGO_URI
    export MONGO_USERNAME=YOUR_MONGO_USERNAME
    export MONGO_PASSWORD=YOUR_MONGO_PASSWORD
    export DB_NAME=YOUR_DATABASE_NAME
  • Now Just, Run the project

    • To the run the project, go to the bash terminal of VSCode or any other code editor and run ./start_server.sh.
    • You donโ€™t have to care about setting .env then yourself then.

๐Ÿ“บ Demo

  • Main screen of the application. image
  • Paste you long URL in the input. image
  • Click on Shorten and copy the short url to clipboard image

๐Ÿค– Author

Github - MBSA-INFINITY LinkedIn - MBSAIADITYA Portfolio - MBSA

Source Code: helpers.py

import string
import random
import re

def random_string_generator(N):
    res = ''.join(random.choices(string.ascii_lowercase +
                             string.digits, k=N))
    return str(res)

def random_string_generator_only_alpha(N):
    res = ''.join(random.choices(string.ascii_lowercase +
                             string.ascii_uppercase, k=N))
    return str(res)


def is_valid_url(url):
    # Define a regular expression pattern to match URLs
    url_pattern = re.compile(
        r'^(https?|ftp)://'  # Match the scheme (http, https, or ftp)
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|'  # Match domain
        r'localhost|'  # Match "localhost"
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # Match IP address
        r'(?::\d+)?'  # Match optional port number
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)

    return bool(url_pattern.match(url))